{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "张三在食堂吃饭\n",
      "李四在办公室吃饭\n"
     ]
    }
   ],
   "source": [
    "#多态\n",
    "#多态是指一类事物有多种形态。比如学生，学生是人的一种，则一个具体的学生就是人的具体形态。\n",
    "class Person: #父类\n",
    "    \"\"\"父类，人类\"\"\"\n",
    "    age=0\n",
    "    name=\"\"\n",
    "    def __init__(self,name,age):\n",
    "        self.name=name\n",
    "        self.age=age\n",
    "    def eat(self):\n",
    "        print(\"{}在吃饭\".format(self.name))\n",
    "class Student(Person):#继承父类\n",
    "    \"\"\"子类，学生\"\"\"\n",
    "    def study(self): #子类方法 增加自己的功能\n",
    "        print(\"{}在学习\".format(self.name))\n",
    "    def eat(self):#重写父类方法，在子类中定义与父类相同名称的方法\n",
    "        print(\"{}在食堂吃饭\".format(self.name))\n",
    "class Teacher(Person):\n",
    "    \"\"\"子类，老师\"\"\"\n",
    "    def teach(self): #子类方法 增加自己的功能\n",
    "        print(\"{}在教书\".format(self.name))\n",
    "    def eat(self):#重写父类方法，在子类中定义与父类相同名称的方法\n",
    "        print(\"{}在办公室吃饭\".format(self.name))\n",
    "#多态\n",
    "def eat(obj):#定义一个函数，参数为任意对象\n",
    "    obj.eat()#调用对象的eat方法\n",
    "s=Student(\"张三\",18)\n",
    "t=Teacher(\"李四\",20)\n",
    "eat(s) #传入不同的对象，执行不同的操作\n",
    "eat(t) #传入不同的对象，执行不同的操作\n",
    "        \n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.0rc2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
